package com.acwing.lc.part1;

import com.acwing.lc.common.ListNode;

import java.util.Comparator;
import java.util.PriorityQueue;

/**
 * @author rkc
 * @date 2022/9/10 15:08
 */
public class LC23合并K个升序链表 {

    private PriorityQueue<ListNode> heap = new PriorityQueue<>(Comparator.comparingInt(o -> o.val));

    public ListNode mergeKLists(ListNode[] lists) {
        ListNode dummy = new ListNode(-1), cur = dummy;
        for (ListNode list : lists) {
            if (list != null) {
                heap.add(list);
            }
        }
        while (!heap.isEmpty()) {
            ListNode t = heap.poll();
            cur = cur.next = t;
            if (t.next != null) {
                heap.add(t.next);
            }
        }
        return dummy.next;
    }
}
